#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""

import math


class Solution(object):
    def closestToTarget(self, arr, target):
        """
        :type arr: List[int]
        :type target: int
        :rtype: int
        """
        self.arr = arr
        self.l = len(arr)

        self.target = target


        self.min = 10**7

        for l in range(0, self.l):
            self.pre = self.arr[l]
            self.update(self.pre)

            for r in range(l+1, self.l):
                self.loop(r)

        return self.min


    def loop(self, r):
        self.pre = self.pre & self.arr[r]
        self.update(self.pre)

    def update(self, pre):
        n = abs(pre-self.target)
        if n < self.min:
            self.min = n



def test():
    s = Solution()
    ret = s.closestToTarget([9,12,3,7,15], 5)
    print(ret)

    ret = s.closestToTarget([1000000,1000000,1000000], 1)
    print(ret)

    ret = s.closestToTarget([1,2,4,8,16], 0)
    print(ret)


def main():
    print("start main")
    test()

if __name__ == "__main__":
    main()
